<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  <title>fs | 个人博客</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="bst 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101">
<meta property="og:type" content="article">
<meta property="og:title" content="fs">
<meta property="og:url" content="https://gitee.com/wieldy/bailment-blog/2022/03/29/fs/index.html">
<meta property="og:site_name" content="个人博客">
<meta property="og:description" content="bst 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2022-03-29T14:12:00.000Z">
<meta property="article:modified_time" content="2023-04-23T12:11:43.651Z">
<meta property="article:author" content="John Doe">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="/bailment-blog/atom.xml" title="个人博客" type="application/atom+xml">
  
  
    <link rel="shortcut icon" href="/bailment-blog/favicon.png">
  
  
    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/typeface-source-code-pro@0.0.71/index.min.css">

  
  
<link rel="stylesheet" href="/bailment-blog/css/style.css">

  
    
<link rel="stylesheet" href="/bailment-blog/fancybox/jquery.fancybox.min.css">

  
<meta name="generator" content="Hexo 6.3.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/bailment-blog/" id="logo">个人博客</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/bailment-blog/">Home</a>
        
          <a class="main-nav-link" href="/bailment-blog/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/bailment-blog/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="https://gitee.com/wieldy/bailment-blog"></form>
      </div>
    </div>
  </div>
</header>

      <div class="outer">
        <section id="main"><article id="post-fs" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-meta">
    <a href="/bailment-blog/2022/03/29/fs/" class="article-date">
  <time class="dt-published" datetime="2022-03-29T14:12:00.000Z" itemprop="datePublished">2022-03-29</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="p-name article-title" itemprop="headline name">
      fs
    </h1>
  

      </header>
    
    <div class="e-content article-entry" itemprop="articleBody">
      
        <ul>
<li><p>bst</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Node</span> &#123;</span><br><span class="line">    <span class="title function_">constructor</span>(<span class="params">element</span>) &#123;</span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">element</span> = element;</span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">left</span> = <span class="literal">null</span>;</span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">right</span> = <span class="literal">null</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Tree</span> &#123;</span><br><span class="line">    <span class="title function_">constructor</span>(<span class="params">compare</span>) &#123;</span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">root</span> = <span class="literal">null</span>;;</span><br><span class="line">        <span class="keyword">let</span> defaultCompare = <span class="variable language_">this</span>.<span class="property">compare</span>;</span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">compare</span> = compare || defaultCompare</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="title function_">compare</span>(<span class="params">a, b</span>) &#123;</span><br><span class="line">        <span class="keyword">return</span> a &lt; b</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="title function_">add</span>(<span class="params">element</span>) &#123;</span><br><span class="line">        <span class="keyword">if</span> (<span class="variable language_">this</span>.<span class="property">root</span> == <span class="literal">null</span>) &#123;</span><br><span class="line">            <span class="variable language_">this</span>.<span class="property">root</span> = <span class="keyword">new</span> <span class="title class_">Node</span>(element);</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="comment">// 这里怎么实现比较好呢？</span></span><br><span class="line">            <span class="keyword">let</span> current = <span class="variable language_">this</span>.<span class="property">root</span>; <span class="comment">// 我们需要不停的找当前节点的左边或者右边</span></span><br><span class="line">            <span class="keyword">let</span> parent = <span class="literal">null</span>;</span><br><span class="line">            <span class="keyword">while</span> (current) &#123;</span><br><span class="line">                parent = current; <span class="comment">// 10</span></span><br><span class="line">                <span class="keyword">if</span> (<span class="variable language_">this</span>.<span class="title function_">compare</span>(current.<span class="property">element</span>, element)) &#123;</span><br><span class="line">                    current = current.<span class="property">right</span>; <span class="comment">// 比当前值小的 放在右边</span></span><br><span class="line">                &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                    current = current.<span class="property">left</span> <span class="comment">// 左边</span></span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> (<span class="variable language_">this</span>.<span class="title function_">compare</span>(parent.<span class="property">element</span>, element)) &#123;</span><br><span class="line">                parent.<span class="property">right</span> = <span class="keyword">new</span> <span class="title class_">Node</span>(element);</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                parent.<span class="property">left</span> = <span class="keyword">new</span> <span class="title class_">Node</span>(element);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="title function_">reverseTree</span>(<span class="params"></span>) &#123;</span><br><span class="line">        <span class="keyword">let</span> stack = [<span class="variable language_">this</span>.<span class="property">root</span>];</span><br><span class="line">        <span class="keyword">let</span> pointer = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">let</span> current;</span><br><span class="line">        <span class="keyword">while</span> (current = stack[pointer++]) &#123;</span><br><span class="line">            <span class="variable language_">console</span>.<span class="title function_">log</span>(current.<span class="property">element</span>);</span><br><span class="line">            <span class="keyword">let</span> temp = current.<span class="property">left</span>; <span class="comment">// 将左边和右边进行一个反转</span></span><br><span class="line">            current.<span class="property">left</span> = current.<span class="property">right</span></span><br><span class="line">            current.<span class="property">right</span> = temp</span><br><span class="line">            <span class="keyword">if</span> (current.<span class="property">left</span>) &#123;</span><br><span class="line">                stack.<span class="title function_">push</span>(current.<span class="property">left</span>);</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> (current.<span class="property">right</span>) &#123;</span><br><span class="line">                stack.<span class="title function_">push</span>(current.<span class="property">right</span>)</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="title function_">levelTraversal</span>(<span class="params">visit</span>) &#123;</span><br><span class="line">        <span class="keyword">let</span> stack = [<span class="variable language_">this</span>.<span class="property">root</span>];</span><br><span class="line">        <span class="keyword">let</span> pointer = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">let</span> current;</span><br><span class="line">        <span class="keyword">while</span> (current = stack[pointer++]) &#123;</span><br><span class="line">            visit.<span class="title function_">visitor</span>(current);</span><br><span class="line">            <span class="keyword">if</span> (current.<span class="property">left</span>) &#123;</span><br><span class="line">                stack.<span class="title function_">push</span>(current.<span class="property">left</span>);</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> (current.<span class="property">right</span>) &#123;</span><br><span class="line">                stack.<span class="title function_">push</span>(current.<span class="property">right</span>)</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 先序（diff算法 遇到节点就比较当前节点，比较后在操作儿子）  中序（按照从小到大或者从大大小来排列的） 后序 （先处理子，在处理父）</span></span><br><span class="line">    <span class="comment">// 按照先中后的顺序来访问树</span></span><br><span class="line">    <span class="comment">// 非递归方式和层序很像 *可以用栈来实现*</span></span><br><span class="line">    <span class="title function_">preTraversal</span>(<span class="params"></span>) &#123;</span><br><span class="line">        <span class="keyword">const</span> <span class="title function_">traversal</span> = (<span class="params">root</span>) =&gt; &#123;</span><br><span class="line">            <span class="keyword">if</span> (root == <span class="literal">null</span>) <span class="keyword">return</span>;</span><br><span class="line">            <span class="variable language_">console</span>.<span class="title function_">log</span>(root);</span><br><span class="line">            <span class="title function_">traversal</span>(root.<span class="property">left</span>);</span><br><span class="line">            <span class="title function_">traversal</span>(root.<span class="property">right</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="title function_">traversal</span>(<span class="variable language_">this</span>.<span class="property">root</span>)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="title function_">inorderTraversal</span>(<span class="params"></span>) &#123;</span><br><span class="line">        <span class="keyword">const</span> <span class="title function_">traversal</span> = (<span class="params">root</span>) =&gt; &#123;</span><br><span class="line">            <span class="keyword">if</span> (root == <span class="literal">null</span>) <span class="keyword">return</span>;</span><br><span class="line">            <span class="title function_">traversal</span>(root.<span class="property">left</span>);</span><br><span class="line">            <span class="variable language_">console</span>.<span class="title function_">log</span>(root);</span><br><span class="line">            <span class="title function_">traversal</span>(root.<span class="property">right</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="title function_">traversal</span>(<span class="variable language_">this</span>.<span class="property">root</span>)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="title function_">postTranversal</span>(<span class="params"></span>) &#123;</span><br><span class="line">        <span class="keyword">const</span> <span class="title function_">traversal</span> = (<span class="params">root</span>) =&gt; &#123;</span><br><span class="line">            <span class="keyword">if</span> (root == <span class="literal">null</span>) <span class="keyword">return</span>;</span><br><span class="line">            <span class="title function_">traversal</span>(root.<span class="property">left</span>);</span><br><span class="line">            <span class="title function_">traversal</span>(root.<span class="property">right</span>);</span><br><span class="line">            <span class="variable language_">console</span>.<span class="title function_">log</span>(root);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="title function_">traversal</span>(<span class="variable language_">this</span>.<span class="property">root</span>)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 树的遍历有四种方式  先序 中序  后序  层序</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 10 8 6</span></span><br><span class="line"><span class="keyword">let</span> tree = <span class="keyword">new</span> <span class="title class_">Tree</span>(<span class="function">(<span class="params">a, b</span>) =&gt;</span> &#123; <span class="comment">//  遍历的时候 提供比较方法 [].sort(()=&gt;&#123;]&#125;)</span></span><br><span class="line">    <span class="keyword">return</span> b.<span class="property">n</span> - a.<span class="property">n</span> &gt; <span class="number">0</span></span><br><span class="line">&#125;);</span><br><span class="line"><span class="keyword">let</span> arr = [&#123; <span class="attr">n</span>: <span class="number">10</span> &#125;, &#123; <span class="attr">n</span>: <span class="number">8</span> &#125;, &#123; <span class="attr">n</span>: <span class="number">19</span> &#125;, &#123; <span class="attr">n</span>: <span class="number">6</span> &#125;, &#123; <span class="attr">n</span>: <span class="number">15</span> &#125;, &#123; <span class="attr">n</span>: <span class="number">22</span> &#125;, &#123; <span class="attr">n</span>: <span class="number">20</span> &#125;]; <span class="comment">// 二叉搜索树要求 存储的数据必须能有比较性</span></span><br><span class="line">arr.<span class="title function_">forEach</span>(<span class="function"><span class="params">item</span> =&gt;</span> &#123;</span><br><span class="line">    tree.<span class="title function_">add</span>(item);</span><br><span class="line">&#125;); <span class="comment">// 如何实现二叉搜索树</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// babel 遍历语法树的时候  babel.transform </span></span><br><span class="line"><span class="comment">// htmlparser</span></span><br><span class="line">tree.<span class="title function_">inorderTraversal</span>(&#123;</span><br><span class="line">    <span class="title function_">visitor</span>(<span class="params">node</span>) &#123;</span><br><span class="line">        <span class="variable language_">console</span>.<span class="title function_">log</span>(node.<span class="property">element</span>.<span class="property">n</span>)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;); <span class="comment">// 树在前端中最常用的操作就是</span></span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(tree.<span class="property">root</span>)</span><br></pre></td></tr></table></figure>
</li>
<li><p>file</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> fs = <span class="built_in">require</span>(<span class="string">&#x27;fs&#x27;</span>).<span class="property">promises</span>;</span><br><span class="line"><span class="keyword">const</span> path = <span class="built_in">require</span>(<span class="string">&#x27;path&#x27;</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 同步阻塞  异步阻塞</span></span><br><span class="line"><span class="comment">// 带sync都是同步的</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 可以使用fs.stat 或者 fs.access 来判断文件的存在性，如果用同步的可以采用fs.exits</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// access 就是看能不能访问到</span></span><br><span class="line"><span class="comment">// stat  文件状态信息 （是文件和是目录都可以）</span></span><br><span class="line"><span class="comment">// fs.stat(path.resolve(__dirname, &#x27;a&#x27;), function (err, statObj) &#123;</span></span><br><span class="line"><span class="comment">//     if (statObj) &#123;</span></span><br><span class="line"><span class="comment">//         console.log(statObj.isFile(), statObj.isDirectory(), statObj.ctime)</span></span><br><span class="line"><span class="comment">//     &#125;</span></span><br><span class="line"><span class="comment">// &#125;);</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// fs.mkdir(path.resolve(__dirname, &#x27;a/b/c/d/e&#x27;), &#123; recursive: true &#125;, function (err) &#123;</span></span><br><span class="line"><span class="comment">//     console.log(err)</span></span><br><span class="line"><span class="comment">// &#125;)</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// fs.rmdir(path.resolve(__dirname, &#x27;a&#x27;), &#123; recursive: true &#125;, function (err) &#123;</span></span><br><span class="line"><span class="comment">//     console.log(err);</span></span><br><span class="line"><span class="comment">// &#125;)</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 读取当前文件夹的子目录</span></span><br><span class="line"><span class="comment">/* fs.readdir(path.resolve(__dirname, &#x27;a&#x27;), function (err, dirs) &#123;</span></span><br><span class="line"><span class="comment">    dirs = dirs.map(dir =&gt; path.join(&#x27;a&#x27;, dir));</span></span><br><span class="line"><span class="comment">    dirs.forEach(dir =&gt; &#123;</span></span><br><span class="line"><span class="comment">        fs.stat(dir, function (err, statObj) &#123;</span></span><br><span class="line"><span class="comment">            if (statObj.isDirectory()) &#123;</span></span><br><span class="line"><span class="comment">                fs.rmdir(dir, () =&gt; &#123; &#125;)</span></span><br><span class="line"><span class="comment">            &#125; else &#123;</span></span><br><span class="line"><span class="comment">                fs.unlink(dir, () =&gt; &#123; &#125;)</span></span><br><span class="line"><span class="comment">            &#125;</span></span><br><span class="line"><span class="comment">        &#125;)</span></span><br><span class="line"><span class="comment">    &#125;);</span></span><br><span class="line"><span class="comment">    // 孩子删除后在删除自己</span></span><br><span class="line"><span class="comment">&#125;) */</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// fs.rmdir(path.resolve(__dirname, &#x27;a&#x27;), function (err) &#123;</span></span><br><span class="line"><span class="comment">//     console.log(err);</span></span><br><span class="line"><span class="comment">// &#125;)</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//  一步步优化逻辑，  就放弃回调。  callback -&gt; async + await</span></span><br><span class="line"><span class="comment">/* function rmdir(dirname, cb) &#123;</span></span><br><span class="line"><span class="comment">    fs.stat(dirname, function (err, statObj) &#123;</span></span><br><span class="line"><span class="comment">        if (err) &#123;</span></span><br><span class="line"><span class="comment">            return cb(err);</span></span><br><span class="line"><span class="comment">        &#125;</span></span><br><span class="line"><span class="comment">        if (statObj.isFile()) &#123;</span></span><br><span class="line"><span class="comment">            fs.unlink(dirname, cb);</span></span><br><span class="line"><span class="comment">        &#125; else &#123;</span></span><br><span class="line"><span class="comment">            // 文件夹</span></span><br><span class="line"><span class="comment">            // 读取文件夹中的内容看一下有没有孩子节点</span></span><br><span class="line"><span class="comment">            fs.readdir(dirname, function (err, dirs) &#123; // 读出 a 目录下的b和c</span></span><br><span class="line"><span class="comment">                dirs = dirs.map(dir =&gt; path.join(dirname, dir)); // 映射路径 a/b  a/c</span></span><br><span class="line"><span class="comment">                let idx = 0;</span></span><br><span class="line"><span class="comment">                function next() &#123;</span></span><br><span class="line"><span class="comment">                    if (idx == dirs.length) return fs.rmdir(dirname, cb); // 子都删除完毕后删除父亲</span></span><br><span class="line"><span class="comment">                    let currentDeleteDir = dirs[idx++]; // 先取出a/b来删除  a/c</span></span><br><span class="line"><span class="comment">                    rmdir(currentDeleteDir, next)</span></span><br><span class="line"><span class="comment">                &#125;</span></span><br><span class="line"><span class="comment">                next(); // 异步迭代需要采用next方法  co 串行</span></span><br><span class="line"><span class="comment">            &#125;)</span></span><br><span class="line"><span class="comment">        &#125;</span></span><br><span class="line"><span class="comment">    &#125;)</span></span><br><span class="line"><span class="comment">&#125; */</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/* function rmdir(dirname, cb) &#123;</span></span><br><span class="line"><span class="comment">    fs.stat(dirname, function (err, statObj) &#123;</span></span><br><span class="line"><span class="comment">        if (err) &#123;</span></span><br><span class="line"><span class="comment">            return cb(err);</span></span><br><span class="line"><span class="comment">        &#125;</span></span><br><span class="line"><span class="comment">        if (statObj.isFile()) &#123;</span></span><br><span class="line"><span class="comment">            fs.unlink(dirname, cb);</span></span><br><span class="line"><span class="comment">        &#125; else &#123;</span></span><br><span class="line"><span class="comment">            // 文件夹</span></span><br><span class="line"><span class="comment">            // 读取文件夹中的内容看一下有没有孩子节点</span></span><br><span class="line"><span class="comment">            fs.readdir(dirname, function (err, dirs) &#123; // 读出 a 目录下的b和c</span></span><br><span class="line"><span class="comment">                dirs = dirs.map(dir =&gt; path.join(dirname, dir)); // 映射路径 a/b  a/c</span></span><br><span class="line"><span class="comment">                if (dirs.length === 0) &#123; // 如果目录就是空的直接删除即可</span></span><br><span class="line"><span class="comment">                    return fs.rmdir(dirname, cb)</span></span><br><span class="line"><span class="comment">                &#125;</span></span><br><span class="line"><span class="comment">                let times = 0;</span></span><br><span class="line"><span class="comment">                function done() &#123;</span></span><br><span class="line"><span class="comment">                    if (++times === dirs.length) &#123;</span></span><br><span class="line"><span class="comment">                        fs.rmdir(dirname, cb)</span></span><br><span class="line"><span class="comment">                    &#125;</span></span><br><span class="line"><span class="comment">                &#125;</span></span><br><span class="line"><span class="comment">                dirs.forEach(dir =&gt; &#123; // a/b  a/c    异步并行 for循环是并行的  递归是串行的</span></span><br><span class="line"><span class="comment">                    rmdir(dir, done)</span></span><br><span class="line"><span class="comment">                &#125;)</span></span><br><span class="line"><span class="comment">            &#125;)</span></span><br><span class="line"><span class="comment">        &#125;</span></span><br><span class="line"><span class="comment">    &#125;)</span></span><br><span class="line"><span class="comment">&#125; */</span></span><br><span class="line"><span class="comment">/* function rmdir(dirname) &#123;</span></span><br><span class="line"><span class="comment">    return new Promise((resolve, reject) =&gt; &#123;</span></span><br><span class="line"><span class="comment">        fs.stat(dirname, function (err, statObj) &#123;</span></span><br><span class="line"><span class="comment">            if (err) &#123;</span></span><br><span class="line"><span class="comment">                return reject(err);</span></span><br><span class="line"><span class="comment">            &#125;</span></span><br><span class="line"><span class="comment">            if (statObj.isFile()) &#123;</span></span><br><span class="line"><span class="comment">                fs.unlink(dirname, resolve);</span></span><br><span class="line"><span class="comment">            &#125; else &#123;</span></span><br><span class="line"><span class="comment">                // 文件夹</span></span><br><span class="line"><span class="comment">                // 读取文件夹中的内容看一下有没有孩子节点</span></span><br><span class="line"><span class="comment">                fs.readdir(dirname, function (err, dirs) &#123; // 读出 a 目录下的b和c</span></span><br><span class="line"><span class="comment">                    Promise.all(dirs.map(dir =&gt; rmdir(path.join(dirname, dir)))).then(() =&gt; &#123;</span></span><br><span class="line"><span class="comment">                        fs.rmdir(dirname, resolve)</span></span><br><span class="line"><span class="comment">                    &#125;);</span></span><br><span class="line"><span class="comment">                &#125;)</span></span><br><span class="line"><span class="comment">            &#125;</span></span><br><span class="line"><span class="comment">        &#125;)</span></span><br><span class="line"><span class="comment">    &#125;)</span></span><br><span class="line"><span class="comment">&#125;</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="comment">// 全部async + await  + promise 来实现代码逻辑</span></span><br><span class="line"><span class="keyword">async</span> <span class="keyword">function</span> <span class="title function_">rmdir</span>(<span class="params">dirname</span>) &#123; <span class="comment">// 异步并发</span></span><br><span class="line">    <span class="keyword">let</span> statObj = <span class="keyword">await</span> fs.<span class="title function_">stat</span>(dirname)</span><br><span class="line">    <span class="keyword">if</span> (statObj.<span class="title function_">isFile</span>()) &#123;</span><br><span class="line">        <span class="keyword">return</span> fs.<span class="title function_">unlink</span>(dirname);</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="keyword">let</span> dirs = <span class="keyword">await</span> fs.<span class="title function_">readdir</span>(dirname);</span><br><span class="line">        <span class="keyword">await</span> <span class="title class_">Promise</span>.<span class="title function_">all</span>(dirs.<span class="title function_">map</span>(<span class="function"><span class="params">dir</span> =&gt;</span> <span class="title function_">rmdir</span>(path.<span class="title function_">join</span>(dirname, dir))))</span><br><span class="line">        <span class="keyword">return</span> fs.<span class="title function_">rmdir</span>(dirname)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 如果是同步的删除 可以考虑层序遍历 + 倒序删除</span></span><br><span class="line"><span class="title function_">rmdir</span>(path.<span class="title function_">resolve</span>(__dirname, <span class="string">&#x27;a&#x27;</span>)).<span class="title function_">then</span>(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;删除成功&#x27;</span>)</span><br><span class="line">&#125;).<span class="title function_">catch</span>(<span class="function"><span class="params">e</span> =&gt;</span> &#123;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(e)</span><br><span class="line">&#125;)</span><br><span class="line"><span class="comment">// fs.stat  fs.readdir  fs.rmdir fs.unlink  遍历</span></span><br></pre></td></tr></table></figure></li>
</ul>
<h2 id="常见的数据结构-（存储数据）"><a href="#常见的数据结构-（存储数据）" class="headerlink" title="常见的数据结构  （存储数据）"></a>常见的数据结构  （存储数据）</h2><ul>
<li>线性结构 查询节点的时候 需要依次遍历 O(n)  线性结构典型的有：队列 、栈 、链表</li>
<li>树结构： 二叉树，存储方式可以通过二分查找来进行搜索  (二叉搜索树)  插入和查询 O(logn)</li>
<li>开发中接触的比较多的。多叉树： DOM树， 文件夹操作（树组件， 节点的访问） babel ast语法树， 路由配置。。。。</li>
</ul>
<blockquote>
<p>二叉搜索树的概念，存储的时候 按照大小进行排列 （二叉排序树）</p>
</blockquote>

      
    </div>
    <footer class="article-footer">
      <a data-url="https://gitee.com/wieldy/bailment-blog/2022/03/29/fs/" data-id="cljmgj1ew000fzc9j5982gdc9" data-title="fs" class="article-share-link">Share</a>
      
      
      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/bailment-blog/2022/03/29/express/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          express
        
      </div>
    </a>
  
  
    <a href="/bailment-blog/2022/03/29/http/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">http</div>
    </a>
  
</nav>

  
</article>


</section>
        
          <aside id="sidebar">
  
    

  
    

  
    
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/bailment-blog/archives/2023/07/">July 2023</a></li><li class="archive-list-item"><a class="archive-list-link" href="/bailment-blog/archives/2023/06/">June 2023</a></li><li class="archive-list-item"><a class="archive-list-link" href="/bailment-blog/archives/2023/04/">April 2023</a></li><li class="archive-list-item"><a class="archive-list-link" href="/bailment-blog/archives/2023/03/">March 2023</a></li><li class="archive-list-item"><a class="archive-list-link" href="/bailment-blog/archives/2022/03/">March 2022</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/bailment-blog/2023/07/03/%E5%B7%A5%E4%BD%9C%E6%97%A5%E5%BF%97/">工作日志</a>
          </li>
        
          <li>
            <a href="/bailment-blog/2023/06/29/payMent/">payMent</a>
          </li>
        
          <li>
            <a href="/bailment-blog/2023/06/20/%E7%BD%91%E7%BA%A2%E9%9D%A2%E8%AF%95%E9%A2%98/">原理题</a>
          </li>
        
          <li>
            <a href="/bailment-blog/2023/06/19/%E5%89%8D%E7%AB%AF%E5%A4%A7%E5%B1%8F%E8%87%AA%E9%80%82%E5%BA%94%E6%96%B9%E6%B3%95/">前端大屏自适应方法</a>
          </li>
        
          <li>
            <a href="/bailment-blog/2023/06/17/10%E4%B8%AAJavaScript%E5%B8%B8%E8%A7%81%E9%AB%98%E7%BA%A7%E7%9F%A5%E8%AF%86%E7%82%B9/">10个JavaScript常见高级知识点</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      
      &copy; 2023 John Doe<br>
      Powered by <a href="https://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>

    </div>
    <nav id="mobile-nav">
  
    <a href="/bailment-blog/" class="mobile-nav-link">Home</a>
  
    <a href="/bailment-blog/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    


<script src="/bailment-blog/js/jquery-3.4.1.min.js"></script>



  
<script src="/bailment-blog/fancybox/jquery.fancybox.min.js"></script>




<script src="/bailment-blog/js/script.js"></script>





  </div>
</body>
</html>